// 记录日志 日志按天记录
const path = require('path')
const fs = require('fs')
const os = require('os')
const dt = require('../utils/date')
const moment = require('moment')

// 记录日志的路径
const logPath = path.join(path.dirname(__dirname), 'logs', `${dt.formDataTime()}.log`)


module.exports = (req, res, next) => {
  console.log(moment().format('YYYY-MM-DD HH:mm:ss'));
  // os.EOL 换行符 根据操作系统自动变  windows \r\n  linux/mac \n
  let data = req.ip + ' -- ' + dt.formDataTime() + ' -- ' + req.method + ' -- ' + req.url + ' -- ' + req.headers['user-agent'] + os.EOL

  fs.appendFileSync(logPath, data)

  next()
}